package com.laycoding.provider.registry;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

@Slf4j
public class ZkRegistry {

    private CuratorFramework curatorFramework;

    public ZkRegistry(CuratorFramework curatorFramework) {
        this.curatorFramework = curatorFramework;
    }

    public void doReginstry(String path, String url, String serviceName) throws Exception {
        Stat stat = curatorFramework.checkExists().forPath(path + url);
        if (stat != null) {
            curatorFramework.delete().forPath(path + url);
        }
        String s = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path + url, serviceName.getBytes());
        log.info("注册结果:--->" + s);
    }
    public void deleteReginstry(String url) throws Exception {
        Stat stat = curatorFramework.checkExists().forPath(url);
        if (stat != null) {
            curatorFramework.delete().forPath(url);
        }
    }

}
